#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include <math.h>
#include "DevicesMemHeap.h"


int main(void)
{
    uint32_t i = 0, j = 0, k = 0, *puiMemHead[256] = {0};

    vMemHeapInit();

    printf("memory heap test.\r\n");

    while(1)
    {
        i = abs(rand()) % 3;
        j = abs(rand()) & 255;
        k = abs(rand()) & 0xFFF;

        /* 新建 */
        if(i == 0)
        {
            if(puiMemHead[j] == NULL)
            {
                puiMemHead[j] = pvMemHeapMalloc(&g_typeMemHeapHead, k);

                if(puiMemHead[j] != NULL)
                {
                    memset(puiMemHead[j], rand(), k);
                }

                printf("new: j: %d,\tpuiMemHead: 0x%08X\r\n", j, (uint32_t)puiMemHead[j]);
            }
        }
        /* 重建 */
        else if(i == 1)
        {
            if(puiMemHead[j] != NULL)
            {
                puiMemHead[j] = pvMemHeapRealloc(&g_typeMemHeapHead, puiMemHead[j], k);

                if(puiMemHead[j] != NULL)
                {
                    memset(puiMemHead[j], rand(), k);
                    printf("rel: j: %d,\tpuiMemHead: 0x%08X\r\n", j, (uint32_t)puiMemHead[j]);
                }
                else
                {
                    printf("rel: j: %d,\tk: %d,\terror.\r\n", j, k);
                }
            }
        }
        /* 删除 */
        else
        {
            if(puiMemHead[j] != NULL)
            {
                printf("del: j: %d,\tpuiMemHead: 0x%08X\r\n", j, (uint32_t)puiMemHead[j]);

                vMemHeapFree(&g_typeMemHeapHead, puiMemHead[j]);
                puiMemHead[j] = NULL;
            }
        }
    }

    return 0;
}
